home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Snippets / DynamoArray 1.0 / DynamoArray.h < prev    next >
Encoding:
Text File  |  1994-05-04  |  6.6 KB  |  187 lines  |  [TEXT/KAHL]

  1. /*
  2.     DynamoArray.h
  3.  
  4.     Version 1.0
  5.     (c)1993, 1994 by Hiep Dam. All Rights Reserved.
  6.     3G Software
  7.     Blah, blah, blah.
  8.  
  9.     ---------------------------------------------------------------------------
  10.  
  11.     December 21, 1993.
  12.     Last update: May 4, 1994.
  13.     Just for your information, I spent 5 hours one night developing this class
  14.     (from midnight to 5 in the morning. I must be crazy).
  15.  
  16.     ---------------------------------------------------------------------------
  17.  
  18.     Dynamic Arrays implemented as a C++ class. Pretty self-explanatory.
  19.     Reason for this class? Well, I needed to maintain a dynamic list of items
  20.     that were to be randomly chosen, one item at a time. However, once that
  21.     item was chosen, it was not to be chosen again. The best way to do
  22.     this was via a dynamic array, getting the item and then deleting it
  23.     and then shrinking the array by 1.
  24.     Note however that in the implementation I don't actually "shrink" the array;
  25.     I just decrement the array size counter.
  26.     The array is allocated via _new.
  27.     
  28.     This is also a good sample to work from if you're new to C++ and OOP.
  29.  
  30.     ---------------------------------------------------------------------------
  31.  
  32.     Updated: 12/24/93
  33.              Added [] operator and fixed minor bug in destructor.
  34.              4/1/94
  35.              Added copy constructor, assignment constructor
  36. */
  37.  
  38.  
  39. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40.  
  41. #ifndef DYNAMOARRAY_H_
  42. #define DYNAMOARRAY_H_
  43.  
  44. typedef long DynamoArrayType;        // Change the type here for your own use
  45.  
  46. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47.  
  48. const short STD_DYNAMO_LEN = 10;    // The standard chunk length the array grows by
  49. const short OUT_OF_BOUNDS = -1;        // Error code returned by the methods
  50.  
  51. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52.  
  53.  
  54.  
  55. // IMPORTANT:
  56. // -----------
  57. // When using arrays, it is always tricky dealing with indices.
  58. // Remember that arrays start with 0, as in anArray[0]. This is different
  59. // from Pascal, where the first item starts at 1, as in anArray[1]. So in C and
  60. // C++ the correct boundaries are anArray[0] to anArray[x - 1], where x is the
  61. // number of items in the array. In Pascal this would be anArray[1] to
  62. // anArray[x]. To clarify things, I use two terms: whole values and index values.
  63. // Whole values range from 1 to the number of items, and index values
  64. // range from 0 to the number of items - 1. So there. No reason for anyone
  65. // to get confused anymore, is there?
  66.  
  67.  
  68.  
  69. typedef class DynamoArray {
  70.     protected:
  71.         short fMaxSize;                // Current length of array    (it's maximum size).
  72.         short fCurSize;                // Number of items in array currently.
  73.         DynamoArrayType *fArray;    // The array itself (actually pointer to the array)
  74.                                     // You can change what the array is by changing
  75.                                     // the definition of DynamoArrayType. By default
  76.                                     // it's just a short (integer).
  77.  
  78.         unsigned short GetRandom(unsigned short min, unsigned short max);
  79.         void IncreaseSize();
  80.  
  81.     public:
  82.         DynamoArray() {
  83.             // Default constructor; no arguments.
  84.             // This constructor just creates an array with the default size
  85.             // of STD_DYNAMO_LEN (i.e. 10). You can change the standard
  86.             // default size by changing STD_DYNAMO_LEN, if you wish.
  87.             fMaxSize = STD_DYNAMO_LEN;
  88.             fCurSize = 0;
  89.             fArray = new DynamoArrayType[fMaxSize];
  90.         } // END constructor
  91.  
  92.         DynamoArray(short size) {
  93.             if (size < 1) {
  94.                 fMaxSize = STD_DYNAMO_LEN;
  95.                 fCurSize = 0;
  96.                 fArray = new DynamoArrayType[fMaxSize];
  97.             }
  98.             else {
  99.                 fMaxSize = size;
  100.                 fCurSize = 0;
  101.                 fArray = new DynamoArrayType[fMaxSize];
  102.             }
  103.         } // END constructor
  104.  
  105.         ~DynamoArray() {
  106.             if (fArray != nil)
  107.                 delete []fArray;
  108.         } // END destructor
  109.         
  110.         // Copy constructor
  111.         DynamoArray(DynamoArray& sourceArray);
  112.         
  113.         // Assignment operator
  114.         DynamoArray operator=(const DynamoArray& sourceArray);
  115.  
  116.         // [].
  117.         // Bracket operator; pass a valid index value (0 to fCurSize - 1);
  118.         DynamoArrayType *operator[] (short index);
  119.  
  120.         // SizeOf.
  121.         // Get how many items are stored in array.
  122.         // Returns a whole value, from 1 to x.
  123.         virtual short SizeOf() { return fCurSize; }
  124.  
  125.         // Get.
  126.         // Specify an index number from 0 to fCurSize - 1. Returns OUT_OF_BOUNDS
  127.         // (-1) if the array is empty; else the index number of the item chosen.
  128.         virtual short Get(DynamoArrayType& theItem, short index);
  129.         
  130.         // RandomGet.
  131.         // Get a random item from the array. The item is NOT deleted from
  132.         // the array. Returns the index value of the item randomly chosen,
  133.         // OUT_OF_BOUNDS (-1) if array is empty.
  134.         virtual short RandomGet(DynamoArrayType& theItem);
  135.         
  136.         // Append.
  137.         // Inserts the specified item at the end of the array. Returns the index
  138.         // value of the item; If the array is initially empty, Append() will
  139.         // create the array itself.
  140.         virtual short Append(const DynamoArrayType& theItem);
  141.  
  142.         // Insert.
  143.         // Inserts the specified item into the array at the specified location.
  144.         // Valid locations are from 0 to fCurSize. Using fCurSize as
  145.         // the location will result the same as Append().
  146.         // Returns OUT_OF_BOUNDS if error occured and unable to insert item; if
  147.         // able to insert item, returns 0.
  148.         virtual short Insert(const DynamoArrayType& theItem, short index);
  149.  
  150.         // Delete.
  151.         // Delete the specified item at array index.
  152.         // Specify an item number from 0 to fCurSize - 1.
  153.         // If the array is empty, Delete() returns OUT_OF_BOUNDS; else
  154.         // the index value of the item deleted.
  155.         virtual short Delete(short index);
  156.  
  157.         // SearchDelete.
  158.         // Remove the specified item in the array, if that item
  159.         // exists. Basically a call to Search(item, x) and then calling Delete(x).
  160.         // Returns OUT_OF_BOUNDS (-1) if item not in array, else the index of the
  161.         // item deleted (from 0 to fCurSize - 1).
  162.         virtual short SearchDelete(DynamoArrayType& theItem, short startIndex = 0);
  163.         
  164.         // Remove.
  165.         // Get the specified item from the array, and then delete that item.
  166.         // Almost the same as calling Get(x) and then calling Delete(x).
  167.         // However, you must pass to Remove the variable that is to hold the
  168.         // deleted item. Item specified should be from 0 to fCurSize - 1.
  169.         // Returns OUT_OF_BOUNDS (-1) if the array is empty, else
  170.         // the array index of the item removed.
  171.         virtual short Remove(DynamoArrayType& theItem, short index);
  172.         
  173.         // RandomRemove.
  174.         // Same as Remove(), except item removed is randomly chosen.
  175.         virtual short RandomRemove(DynamoArrayType& theItem);
  176.  
  177.         // Search.
  178.         // Looks for specified item in the array.
  179.         // Returns OUT_OF_BOUNDS (-1) if not found, else 0 to fCurSize - 1.
  180.         // If your DynamoArrayType is a complex data structure (such as a class)
  181.         // it is recommended you overload the == (equality) operator.
  182.         virtual short Search(const DynamoArrayType& source, short startIndex = 0);
  183. }; // END DynamoArray
  184.  
  185. // END DynamoArray.h
  186.  
  187. #endif // DYNAMOARRAY_H_